package net.lainiao.mycost.controller;

import net.lainiao.mycost.bean.ExpenseAccount;
import net.lainiao.mycost.bean.UserInfo;
import net.lainiao.mycost.utils.Comm;
import net.lainiao.mycost.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
public class LoginController extends Page {

    @RequestMapping("login.do")
    public String login(UserInfo user, HttpSession session, Model model) {
        UserInfo userInfo = userInfoService.isLogin(user);
        if (userInfo != null) {
            session.setAttribute("user", userInfo);
            List<Map> list = anthorityService.getMenuList(userInfo.getRoleId());
            session.setAttribute("menulist", list);
            return "main/index";
        }
        return "redirect:login.jsp";
    }

    @RequestMapping("bootm.do")
    public String bootm(Model model, HttpSession session) {
        boolean mmark = false;
        boolean finMafrk = false;
        boolean reportMark = false;
        List<Map> list = (List<Map>) session.getAttribute("menulist");
        if (list != null) {

            for (int i = 0; i < list.size(); i++) {
                Map map = list.get(i);
                int menuid = Integer.parseInt(map.get("menu_id").toString());
                if (menuid == 12) {
                    mmark = true;
                    break;
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Map map = list.get(i);
                int menuid = Integer.parseInt(map.get("menu_id").toString());
                if (menuid == 13) {
                    finMafrk = true;
                    break;
                }
            }

            for (int i = 0; i < list.size(); i++) {
                Map map = list.get(i);
                int menuid = Integer.parseInt(map.get("menu_id").toString());
                if (menuid == 20) {
                    reportMark = true;
                    break;
                }

            }

        }
        if (reportMark) {
            List<Map> salaryList = reportService.salaryMonthReport();
            model.addAttribute("salarylist", salaryList);
            List<Map> expenselist = reportService.expenseMonthReport();
            model.addAttribute("expenselist", expenselist);
        }
        if (mmark) {
            ExpenseAccount account = new ExpenseAccount();
            account.setExpenseState(Comm.EXPPENSE_STATE_ONE);
            account.setStart(-1);
            model.addAttribute("managerlist", expenseService.list(account));
        }
        if (finMafrk) {
            ExpenseAccount account = new ExpenseAccount();
            account.setExpenseState(Comm.EXPPENSE_STATE_TWO);
            account.setStart(-1);
            model.addAttribute("financelist", expenseService.list(account));
        }
        return "main/bootm";
    }

    @RequestMapping("exit.do")
    public String exit(HttpSession session) {
        session.removeAttribute("user");
        return "rediret:login.jsp";
    }

    @RequestMapping("user.do")
    public String loaduser() {
        return "main/user_info";
    }


}
